﻿using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace news.admin.cyt369.com.services.Services
{
    public class ArticlesService : admin.cyt369.com.services.ORM.BaseService<ArticlesService>
    {
        public ArticlesService() { }


        /// <summary>
        /// 文章查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="total"></param>
        /// <param name="title"></param>
        /// <param name="auditStatusId"></param>
        /// <param name="contentTypeId"></param>
        /// <param name="isDeleted"></param>
        /// <returns></returns>
        public IQueryable<news.admin.cyt369.com.services.Models.Document> GetArticles(int page, int rows, ref int total,
                                                                                      string title = null, int? auditStatusId = null, 
                                                                                      int? contentTypeId = null, bool isDeleted = false)
        {
            var items = base.Get<news.admin.cyt369.com.services.Models.Document>(c=>c.Deleted == isDeleted);
            if (auditStatusId.HasValue)
            {
                items = items.Where(c=>c.AuditStatusID == auditStatusId.Value);
            }

            if (contentTypeId.HasValue)
            {
                items = items.Where(c => c.ContentType == contentTypeId.Value);
            }

            if (!string.IsNullOrEmpty(title))
            {
                items = items.Where(c => c.Title.Contains(title));
            }

            return this.Pagination(page, rows, ref total, items, c => c.Id);

        }

        /// <summary>
        /// 获取单个文章
        /// </summary>
        /// <param name="docId"></param>
        /// <param name="auditStatusId"></param>
        /// <returns></returns>
        public news.admin.cyt369.com.services.Models.Document GetArticleById(long docId, int? auditStatusId = null)
        {
            using (var db = this.Ctx)
            {
                var query = from o in db.Set<news.admin.cyt369.com.services.Models.Document>().Include(c => c.DocExt)
                                                                                              .Include(c => c.Attachments)
                                                                                              .Include(c=>c.DocumentAudits)
                            where o.Id == docId
                            select o;

                if (auditStatusId.HasValue)
                {
                    query = query.Where(c=>c.AuditStatusID == auditStatusId.Value);
                }

                return query.FirstOrDefault();
            }
        }


        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="docId"></param>
        /// <returns></returns>
        public bool DeleteDocument(long docId)
        {
            using (var db = this.Ctx)
            {
                var item = (from o in db.Set<news.admin.cyt369.com.services.Models.Document>()
                            where o.Id == docId
                            select o).FirstOrDefault();

                if (item != null)
                {
                    item.Deleted = true;
                    db.SaveChanges();
                }

                return true;
            }

        }

        /// <summary>
        /// 执行审核
        /// </summary>
        /// <param name="docId"></param>
        /// <param name="canComment"></param>
        /// <param name="auditStatusId"></param>
        /// <param name="auditUserId"></param>
        /// <param name="auditRemark"></param>
        /// <returns></returns>
        public bool DoArticleAudit(long docId, bool canComment, int auditStatusId, long auditUserId, string auditRemark)
        {
            using (var db = this.Ctx)
            {
                var doc = (from o in db.Set<news.admin.cyt369.com.services.Models.Document>().Include(c => c.DocExt)
                           where o.Id == docId
                           select o).FirstOrDefault();

                if (doc != null)
                {
                    doc.AuditStatusID = auditStatusId;
                    doc.Published = (auditStatusId==1);
                    doc.PublishTime = DateTime.Now;
                    doc.CanComment = canComment;

                    db.Set<news.admin.cyt369.com.services.Models.DocumentAudit>().Add(new news.admin.cyt369.com.services.Models.DocumentAudit() { 
                        DocID = doc.Id,
                        AuditStatusID = auditStatusId,
                        AuditUserID = auditUserId,
                        AuditRemark = auditRemark,
                        AuditTime = DateTime.Now
                    });
                    
                    db.SaveChanges();

                    return true;
                }
            }

            return false;
        }


        /// <summary>
        /// 查询审核记录
        /// </summary>
        /// <param name="docId"></param>
        /// <returns></returns>
        public IQueryable<news.admin.cyt369.com.services.Models.DocumentAudit> GetDocumentAudits(long docId)
        {
            var items = base.Get<news.admin.cyt369.com.services.Models.DocumentAudit>(c=>c.DocID == docId);
            return items.OrderByDescending(c=>c.Id);
        }
    }
}
